package com.banshuibao.bsb_android.activity;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.view.View;

import com.banshuibao.bsb_android.util.PermissionUtil;
import com.banshuibao.bsb_android.util.XToast;

import java.util.HashMap;

/**
 * Created by zyh on 2017/2/17.
 */

public abstract class PermissionBaseActivity extends BaseActivity {

    private static final int REQUEST_EXTERNAL_STORAGE = 10000;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    private int permissionRequestCode = 10001;

    /*
    * 设置权限名称
    * **/
    private static HashMap<String, String> permissionName = new HashMap<>();

    static {
        permissionName.put(Manifest.permission.WRITE_EXTERNAL_STORAGE, "读写手机存储权限");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initPermissionHUAWEI();
    }

    /**
     * 兼容华为手机
     **/
    private void initPermissionHUAWEI() {
        int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (permission != PackageManager.PERMISSION_GRANTED) {
            // We don't have permission so prompt the user
            ActivityCompat.requestPermissions(
                    this,
                    PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE
            );
        }
    }

    /**
     * 请求单个权限成功后使用successGrantPermission();
     **/
    public void requestUserPermission(View view, final String permission, final int request) {
        if (request != REQUEST_EXTERNAL_STORAGE) permissionRequestCode = request;
        if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
                Snackbar.make(view, "权限未开始，是否打开？", Snackbar.LENGTH_INDEFINITE).setAction("ok", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ActivityCompat.requestPermissions(context, new String[]{permission}, request);
                    }
                }).show();
            } else {
                ActivityCompat.requestPermissions(context, new String[]{permission}, request);
            }
        } else {
            successGrantPermission(request);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (permissionRequestCode == requestCode) {
            if (PermissionUtil.verifyPermissions(grantResults)) {
                successGrantPermission(requestCode);
            } else {
                if (permissions.length>0&&permissionName.containsKey(permissions[0]))
                    XToast.showToast(context, "办税宝需要" + permissionName.get(permissions[0]));
                else
                    XToast.showToast(context, "办税宝需要开启相关权限才可以使用此功能");
            }

//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//                if (!shouldShowRequestPermissionRationale(permissions[0])) {
//                 //   AskForPermission();
//                }
//            }
        }
    }


    /**
     * 权限申请通过后
     **/
    public abstract void successGrantPermission(int requestCode);

    public  void AskForPermission() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("需要手动打开相关权限!");
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.setPositiveButton("设置", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.setData(Uri.parse("package:" + getPackageName())); // 根据包名打开对应的设置界面
                startActivity(intent);
            }
        });
        builder.create().show();
    }

}
